/*
 * Copyright (c) 2021-2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.yuki312.backoff.example.slice;

import com.yuki312.backoff.example.LogUtil;
import com.yuki312.backoff.example.ResourceTable;
import com.yuki312.backoff.example.RetrofitSample;
import com.yuki312.backoff.ktx.BackoffExt;
import com.yuki312.backoff.rxjava2.RxBackoff;

import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.schedulers.Schedulers;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;

public class MainAbilitySlice extends AbilitySlice {
    private final String TAG = "RxBackoffMainAbility";

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_main);
        Observable.error(new Exception("sample"))
                .doOnError(new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        LogUtil.info(TAG, " accept:" + throwable.getLocalizedMessage());
                    }
                })
                .retryWhen(RxBackoff.of(BackoffExt.binaryExponentialBackoff(), 5).observable())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object object) {
                        LogUtil.info(TAG, "subscribe accept...");
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) {
                        LogUtil.info(TAG, " accept error:" + throwable.getMessage());
                        throwable.printStackTrace();
                    }
                });
        //Testcase2: download Image by applying retry exponential backoff
        new RetrofitSample(getContext()).downloadImage();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }


}
